package test;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;

/**
 * 查询同航线下的不同航班的不同航班,价格和可预定车位
 */

public class ShowMultTableInfo extends JFrame {
    private JTable table;
    private JButton buttonAdd, buttonAlter, buttonDelete;

    public ShowMultTableInfo() {
        setTitle("多表信息页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        //同航线下的不同航班

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("航线id");
        model.addColumn("航线起点");
        model.addColumn("航线终点");

        model.addColumn("航班id");
        model.addColumn("航班价格");
        model.addColumn("可用座位");
        model.addColumn("出发时间");
        model.addColumn("到达时间");



        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT air_airway.airway_id,air_airway.departure,air_airway.arrival," +
                    "air_flight.flight_id,air_flight.price,air_flight.available_seat,air_flight.departure_time,air_flight.arrival_time " +
                    "FROM air_airway,air_flight  " +
                    "WHERE air_airway.airway_id=air_flight.airway_id  " +

                    "ORDER BY air_flight.departure_time ASC");
//            select * from emp,dept where emp.dep_id=dept.id;
            while (resultSet.next()) {
                int airwayId = resultSet.getInt("airway_id");
                String departure = resultSet.getString("departure");
                String arrival = resultSet.getString("arrival");

                int flightId = resultSet.getInt("flight_id");
                String price = resultSet.getString("price");
                String availableSeat = resultSet.getString("available_seat");
                String departureTime = resultSet.getString("departure_time");
                String arrivalTime = resultSet.getString("arrival_time");


                Object[] rowData = {airwayId, departure, arrival, flightId, price, availableSeat,departureTime,arrivalTime};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 添加刷新按钮
        buttonAdd = new JButton("添加数据");
        buttonAlter = new JButton("修改数据");
        buttonDelete = new JButton("删除数据");
        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        Panel.add(buttonAdd);
        Panel.add(buttonAlter);
        Panel.add(buttonDelete);
        add(Panel, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ShowMultTableInfo userInfoView = new ShowMultTableInfo();
            userInfoView.setSize(400, 300);
            userInfoView.setVisible(true);
        });
    }
}